import sys
import requests
import os
import time

import pytest

from common.mail import Mail
from common.result import Result
from common.webkeyword import WebKeyword
from excel_ddt import ddt
from get_path import get_pathdir
#定义python系统变量
# JOB_URL = sys.argv[1]
# JOB_NAME = sys.argv[2]
# JOB_URL = "http://localhost:8080/job/web"
# JOB_NAME = "web"
JOB_URL = sys.argv[1]
JOB_NAME = sys.argv[2]
BUILD_NUMBER=sys.argv[3]



# 飞书机器人的webhook地址
url = 'https://open.feishu.cn/open-apis/bot/v2/hook/3cfe9125-a44b-49b4-af0b-09ad73de6044'
method = 'post'
headers = {'Content-Type':'application/json'}
res = Result()
result = res.get_res('\\lib/cases\\result-广告批创.xlsx')
print(result)
runtype = result.get("runtype")
title = result.get("title")
starttime = result.get("starttime")
endtime = result.get("endtime")
casecount = result.get("casecount")
passrate = result.get("passrate")
status=result.get("status")
if status=='PASS':
    BUILD_STATUS = "成功"
else:
    BUILD_STATUS = "失败"
with open(get_pathdir + r'\lib\module1.html', mode='r', encoding='utf8') as f:
    text = f.read()
    if status=="FAIL":
        text = text.replace("scolor", "red")
    else:
        text = text.replace("scolor", "green")
    text = text.replace("status", f"{status}")
    text = text.replace("passrate", f"{passrate}")
    text = text.replace("casecount", f"{casecount}")
    text = text.replace("endtime", f"{endtime}")
    text = text.replace("starttime", f"{starttime}")
    text = text.replace("title", f"{title}")
    text = text.replace("runtype", f"{runtype}")
raw_index = ""
groups = res.get_groups('\\lib\\cases\\result-广告批创.xlsx')[1:]
print(groups)
for group in groups:
    index_text = '<tr><td width="100" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">分组信息</td><td width="80" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">用例总数</td><td width="80" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">通过数</td><td width="80" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;color: green;">状态</td></tr>'
    group_info = group[0]
    case_num = group[1]
    pass_num = group[2]
    status = group[3]
content=f"第{BUILD_NUMBER}次构建，构建结果：{BUILD_STATUS}\n" \
            f"测试类型：{runtype}\n" \
            f"测试标题：{title}\n" \
            f"测试状态：{status}\n" \
            f"总用例数：{casecount}\n" \
            f"通过率：{passrate}\n" \
            f"开始时间：{starttime}\n" \
            f"结束时间：{endtime}\n" \
            "\n " \
            "\n " \
            f"----------------------------------以下是测试结果汇总---------------------------------- \n" \


if len(groups)>0:
    # content+="           分组信息             用例总数  通过数  状态  \n"
    content += "                             分组信息                      用例总数           通过数            状态  \n"


    for group in groups:
        group_info = group[0]
        len_blank = 20 - len(group_info)
        print(f"{group_info}:{len_blank}")
        case_num = group[1]
        pass_num = group[2]
        status = group[3]
        # content += f"{group_info}" + len_blank * "--" + f"{case_num}    {pass_num}    {status}    \n"
        content += f"{group_info}" + len_blank * "---" + f"{case_num}                      {pass_num}                 {status}    \n"

data = {
    "msg_type": "interactive",
    "card": {
        "config": {
                "wide_screen_mode": True,
                "enable_forward": True
        },
        "elements": [{
                "tag": "div",
                "text": {
                        "content": content, # 这是卡片的内容，也可以添加其他的内容：比如构建分支，构建编号等
                        "tag": "lark_md"
                }
        }, {
                "actions": [{
                        "tag": "button",
                        "text": {
                                "content": "查看allure测试报告", # 这是卡片的按钮，点击可以跳转到url指向的allure路径
                                "tag": "lark_md"
                        },
                        "color": "green",
                        "url": f"{JOB_URL}{BUILD_NUMBER}/allure/", # JOB_URL 调用python定义的变量，该url是服务器下的allure路径
                        "type": "default",
                        "value": {}
                }],
                "tag": "action"
        }],
        "header": {
                "title": {
                        "content": JOB_NAME + "构建报告", # JOB_NAME 调用python定义的变量，这是卡片的标题
                        "tag": "plain_text"
                }
        }
    }
}


res= requests.request(method=method,url=url,headers=headers,json=data)
print(res)
print(res.json())